package com.service.system.partner.impl;

import com.dao.PartnerMapper;
import com.dao.UserInfoMapper;
import com.dao.UserInviteMapper;
import com.entity.PartnerListVo;
import com.entity.PartnerOrderVo;
import com.entity.system.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.system.partner.PartnerService;
import com.util.Const;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PartnerServiceImpl implements PartnerService {

    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    PartnerMapper partnerMapper;

    @Autowired
    UserInviteMapper userInviteMapper;

    @Override
    public Map<String, Object> findPartnerList(Map<String, Object> map) {
        Map<String, Object> maps = new HashMap<>();
        int pageNum = 1;
        int pageSize = 10;
        if (StringUtils.isNotEmpty((String) map.get("pageNum"))) {
            pageNum = Integer.parseInt((String) map.get("pageNum"));
        }
        if (StringUtils.isNotEmpty((String) map.get("pageSize"))) {
            pageSize = Integer.parseInt((String) map.get("pageSize"));
        }
        //查询合伙人
        map.put("role", "HEHUOREN");
        PageHelper.startPage(pageNum, pageSize);
        List<UserInfo> list = userInfoMapper.selectList(map);
        PageInfo<UserInfo> pageInfo = new PageInfo<>(list);
        List<UserInfo> partnerList = pageInfo.getList();
//        List<PartnerListVo> listVo = new ArrayList<>();
//        for (UserInfo ui : partnerList) {
//            //合伙人列表
//            PartnerListVo plv = new PartnerListVo();
//            plv.setId(ui.getId());
//            plv.setNickName(ui.getNickName());
//            listVo.add(plv);
//        }
        maps.put("code", Const.SUCCESS_CODE);
        maps.put("message", "查询成功");
        maps.put("list", partnerList);
        maps.put("page", pageInfo);
        return maps;
    }

    @Override
    public Map<String, Object> partnerOrder(Map<String, Object> map) {
        Map<String, Object> maps = new HashMap<>();
        int pageNum = 1;
        int pageSize = 10;
        if (StringUtils.isNotEmpty((String) map.get("pageNum"))) {
            pageNum = Integer.parseInt((String) map.get("pageNum"));
        }
        if (StringUtils.isNotEmpty((String) map.get("pageSize"))) {
            pageSize = Integer.parseInt((String) map.get("pageSize"));
        }
        int order = pageNum * pageSize - 10;
        List<PartnerOrderVo> list;
        //排行类型 1 推荐排行   2 流水排行
        String flag = (String) map.get("flag");
        if ("1".equals(flag)) {
            //推荐排行 order_type == 1
            PageHelper.startPage(pageNum, pageSize);
            list = partnerMapper.partnerOrder(1);
        } else {
            //流水排行 order_type == 2
            PageHelper.startPage(pageNum, pageSize);
            list = partnerMapper.partnerOrder(4);
        }

        PageInfo<PartnerOrderVo> pageInfo = new PageInfo<>(list);
        List<PartnerOrderVo> partnerList = pageInfo.getList();
        List<PartnerOrderVo> listVo = new ArrayList<>();
        for (PartnerOrderVo ui : partnerList) {
            //合伙人排行
            PartnerOrderVo pov = new PartnerOrderVo();
            pov.setId(ui.getId());
            pov.setNickName(ui.getNickName());
            Long price = ui.getPrice();
            pov.setPrice(price != null ? price / 100 : 0);
            order++;
            pov.setOrder(order);
            listVo.add(pov);
        }
        maps.put("code", Const.SUCCESS_CODE);
        maps.put("message", "查询成功");
        maps.put("list", listVo);
        maps.put("page", pageInfo);
        return maps;
    }

    @Override
    public Map<String, Object> findInviterUserList(String userId,int pageSize,int pageNum) {
        Map<String,Object> map = new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        Map<String,String> map1 = new HashMap<>();
        map1.put("userId",userId);
        List<UserInvite> list = userInviteMapper.selectPageList(map1);
        PageInfo<UserInvite> pageInfo = new PageInfo<>(list);
        List<UserInvite> userInvites = pageInfo.getList();
        for (UserInvite userInvite : userInvites){
            UserInfo userInfo = userInfoMapper.findUserById(userInvite.getNewUserId());
            if(null != userInfo){
                userInvite.setPhone(userInfo.getPhone());
                userInvite.setNickName(userInfo.getNickName());
            }
        }
        map.put("code",Const.SUCCESS_CODE);
        map.put("message","查询成功");
        map.put("userInvites",userInvites);
        map.put("page",pageInfo);
        return map;
    }
}
